package util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import modelUtil.Configuracion;
import plantilla.BotonTO;
import AccionBoton.AsignarAccion;
import AccionBoton.accionRaton;

public class BotonPersonalizado extends JButton{

	private static final long serialVersionUID = 1;
	private Color colorFondo;
	private Color colorPresionado;
	private Color colorMouseOver;
	private Color colorPrimigenio;
	private BotonTO botonInfo;
	private Color antiguo;
	private JLabel labelImagen = new JLabel();
	private JLabel labelTexto = new JLabel();
	private String alineacionTexto;
	private String alineacionImagen;
	private int horizontalTexto;
	private int verticalTexto;
	private int horizontalImagen;
	private int verticalImagen;
	private TecladoReal _teclado;

	public BotonPersonalizado(){
		
		super();
		colorFondo = Color.white;
		colorPresionado = Color.orange;
		colorMouseOver = Color.orange;
		colorPrimigenio = colorFondo;
		setContentAreaFilled(false);
		
	}
	
	public BotonPersonalizado(Color fon,Color pre,BotonTO boton){
		super();
		colorFondo = fon;
		colorPrimigenio = colorFondo;
		colorPresionado = pre;
		setContentAreaFilled(false);
		botonInfo = boton;
		configurar();
	}
	
	public BotonPersonalizado(Color fon,Color pre,BotonTO boton,TecladoReal teclado){
		super();
		colorFondo = fon;
		colorPrimigenio = colorFondo;
		colorPresionado = pre;
		setContentAreaFilled(false);
		_teclado = teclado;
		botonInfo = boton;
		configurar();
	}
	
	public BotonPersonalizado(Color fon,Color pre,BotonTO boton, String preview){
		super();
		colorFondo = fon;
		colorPrimigenio = colorFondo;
		colorPresionado = pre;
		setContentAreaFilled(false);
		botonInfo = boton;
		configurarPreview();
	}
	
	
	protected void paintComponent( Graphics g ){
		if(getModel().isArmed()){
			g.setColor(colorPresionado);
		}else{
			g.setColor(colorFondo);
		}
		g.fillRect(0, 0, getSize().width-1,getSize().height-1);
		super.paintComponent(g);
	}
	
	public Color getBackground() {
		return colorFondo;
	}

	public void setBackground(Color colorFondo) {
		this.colorFondo = colorFondo;
		this.colorPrimigenio = colorFondo;
		super.setBackground(colorFondo);

	}
	
	public void setPresionado(Color colorFondo) {
		this.colorPresionado = colorFondo;
	}
	
	public Color getPresionado(){	
		return colorPresionado;
	}
	
	public void setBorder(Border b){	
		super.setBorder(b);
	}
	
	public Color getColorMouseOver() {
		return colorMouseOver;
	}

	public void setColorMouseOver(Color colorMouseOver) {
		this.colorMouseOver = colorMouseOver;
	}
	
	public void seleccionar(){
		this.antiguo = colorFondo;
		colorFondo = colorMouseOver;
		super.setBackground(colorFondo);
	}
	
	public void entrar(){
		
		colorFondo = colorMouseOver;
		super.setBackground(colorFondo);
	}
	
	public void salir(){
		
		colorFondo = colorPrimigenio;
		super.setBackground(colorFondo);
	}
	
	public void deseleccionar(){
		colorFondo = colorPrimigenio;
		antiguo = null;
		super.setBackground(colorFondo);
	}

	public Color getColorPrimigenio() {
		return colorPrimigenio;
	}

	public void setColorPrimigenio(Color colorPrimigenio) {
		this.colorPrimigenio = colorPrimigenio;
	}

	public BotonTO getBotonInfo() {
		return botonInfo;
	}

	public void setBotonInfo(BotonTO botonInfo) {
		this.botonInfo = botonInfo;
	}
	public void configurar(){
		
		this.setName(botonInfo.getNombreBoton());
		GridBagLayout grid3 = new GridBagLayout();
		GridBagConstraints cons = new GridBagConstraints();
		
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		
		alineacionTexto = botonInfo.getTextAlign();
		alineacionImagen = botonInfo.getImageAlign();
		configurarAlineacion();


		labelImagen.setHorizontalAlignment(horizontalImagen);
		labelImagen.setVerticalAlignment(verticalImagen);
		//labelImagen.setVerticalAlignment(SwingConstants.TOP);

		labelTexto.setHorizontalAlignment(horizontalTexto);
		labelTexto.setVerticalAlignment(verticalTexto);

		labelTexto.setText(botonInfo.getTextoBoton());
		labelTexto.setForeground(new Color(new Integer(botonInfo.getNombreColorFuente())));
		String[] tamaño = botonInfo.getTamanoFuente().split(",");
		labelTexto.setFont(new Font(botonInfo.getNombreFuente(), Font.PLAIN, new Integer(tamaño[0])));
		//labelTexto.setFont(new Font(botonInfo.getNombreFuente(), Font.PLAIN, 12));

		ImageIcon imagenicono;
		//ImageIcon imagenicono = new ImageIcon(Configuracion.instancia().getDirUsuarioActual() + "//" +Configuracion.instancia().getTecladoActual() + "//" + botonInfo.getNombreIconoFichero());

		if(botonInfo.getNombreIconoFichero().contains("/")){
    		imagenicono = new ImageIcon(botonInfo.getNombreIconoFichero());

		}else{
			
		imagenicono = new ImageIcon(Configuracion.instancia().getDirUsuarioActual() + "//" +Configuracion.instancia().getTecladoActual() + "//" + botonInfo.getNombreIconoFichero());

		}
		/*
    	int alto = imagenicono.getIconHeight();
    	int ancho = imagenicono.getIconWidth();
    	//double factorEscala = (double)11*(alto /  ((screenSize.getHeight() /3) - 80));

		//ancho = ((int) (ancho / factorEscala));
		//alto = ((int) (alto / factorEscala));
		Image redimensionada = imagenicono.getImage().getScaledInstance(ancho,
				alto, Image.SCALE_DEFAULT);
		imagenicono = new ImageIcon(redimensionada);*/
				
		labelImagen.setIcon(imagenicono);
		this.setLayout(grid3);
     	cons.fill = GridBagConstraints.BOTH;
        cons.weightx = 1.0;
        cons.weighty = 1.0;
        cons.gridx=0;
        cons.gridy=0;
        grid3.setConstraints(labelTexto, cons);
        cons.gridx=0;
        cons.gridy=0;
        grid3.setConstraints(labelImagen, cons);

		this.add(labelTexto);
		this.add(labelImagen);

		
		
		labelTexto.getAccessibleContext().setAccessibleName(botonInfo.getAccesibleName());
		labelTexto.getAccessibleContext().setAccessibleDescription(botonInfo.getAccesibleDescription());
		this.setToolTipText(botonInfo.getAccesibleName());
		setBorder(new LineBorder(new Color(new Integer(botonInfo.getNombreColorBorde())), new Integer(botonInfo.getBorderSize())));
		setBackground(new Color(new Integer(botonInfo.getNombreColorFondo()),true));
		setPresionado(new Color(new Integer(botonInfo.getNombreColorMouseDown())));
		setColorPrimigenio(new Color(new Integer(botonInfo.getNombreColorFondo()),true));
		setColorMouseOver(new Color(new Integer(botonInfo.getNombreColorMouseOver())));
		addMouseListener(new accionRaton(this, new Color(new Integer(botonInfo.getNombreColorMouseOver()))));
		addActionListener(new AsignarAccion(this, botonInfo.getAccionEjecutar(), _teclado));
		
		
	}
	
	
	public void configurarPreview(){
		
		this.setName(botonInfo.getNombreBoton());
		GridBagLayout grid3 = new GridBagLayout();
		GridBagConstraints cons = new GridBagConstraints();
				
		
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		
		alineacionTexto = botonInfo.getTextAlign();
		alineacionImagen = botonInfo.getImageAlign();
		configurarAlineacion();


		labelImagen.setHorizontalAlignment(horizontalImagen);
		labelImagen.setVerticalAlignment(verticalImagen);

		labelTexto.setHorizontalAlignment(horizontalTexto);
		labelTexto.setVerticalAlignment(verticalTexto);
		labelTexto.setText(botonInfo.getTextoBoton());
		labelTexto.setForeground(new Color(new Integer(botonInfo.getNombreColorFuente())));
		String[] tamaño = botonInfo.getTamanoFuente().split(",");
		labelTexto.setFont(new Font(botonInfo.getNombreFuente(), Font.PLAIN, new Integer(tamaño[0])));

		String ruta = botonInfo.getNombreIconoFichero();
		int indice = ruta.lastIndexOf("/");
		ImageIcon imagenicono;
		if(indice==-1){
		
		imagenicono = new ImageIcon(Configuracion.instancia().getDirUsuarioActual() + "//" +Configuracion.instancia().getTecladoActual() + "//" + botonInfo.getNombreIconoFichero());
		}else{
			
			imagenicono = new ImageIcon(botonInfo.getNombreIconoFichero());

			
		}
    	int alto = imagenicono.getIconHeight();
    	int ancho = imagenicono.getIconWidth();
    	double factorEscala = (double)11*(alto /  ((screenSize.getHeight() /3) - 80));

		ancho = ((int) (ancho / factorEscala));
		alto = ((int) (alto / factorEscala));
		Image redimensionada = imagenicono.getImage().getScaledInstance(ancho,
				alto, Image.SCALE_DEFAULT);
		imagenicono = new ImageIcon(redimensionada);
		
		

		labelImagen.setIcon(imagenicono);
		this.setLayout(grid3);
     	cons.fill = GridBagConstraints.BOTH;
        cons.weightx = 1.0;
        cons.weighty = 1.0;
        cons.gridx=0;
        cons.gridy=0;
        grid3.setConstraints(labelTexto, cons);
        cons.gridx=0;
        cons.gridy=0;
        grid3.setConstraints(labelImagen, cons);


		
		
		this.add(labelTexto);
		this.add(labelImagen);

		
		
		labelTexto.getAccessibleContext().setAccessibleName(botonInfo.getAccesibleName());
		labelTexto.getAccessibleContext().setAccessibleDescription(botonInfo.getAccesibleDescription());
		this.setToolTipText(botonInfo.getAccesibleName());
		setBorder(new LineBorder(new Color(new Integer(botonInfo.getNombreColorBorde())), new Integer(botonInfo.getBorderSize())));
		setBackground(new Color(new Integer(botonInfo.getNombreColorFondo()),true));
		setPresionado(new Color(new Integer(botonInfo.getNombreColorMouseDown())));
		setColorPrimigenio(new Color(new Integer(botonInfo.getNombreColorFondo()),true));
		setColorMouseOver(new Color(new Integer(botonInfo.getNombreColorMouseOver())));
		
	}
	

	public JLabel getLabelImagen() {
		return labelImagen;
	}

	public void setLabelImagen(JLabel labelImagen) {
		this.labelImagen = labelImagen;
	}

	public JLabel getLabelTexto() {
		return labelTexto;
	}

	public void setLabelTexto(JLabel labelTexto) {
		this.labelTexto = labelTexto;
	}
	
	public void configurarAlineacion(){
		
		if(alineacionTexto.equals("TopLeft")){
			
			horizontalTexto = SwingConstants.LEFT;
			verticalTexto = SwingConstants.TOP;
			
		}
		
		if(alineacionTexto.equals("TopCenter")){
			
			horizontalTexto = SwingConstants.CENTER;
			verticalTexto = SwingConstants.TOP;
			
		}
		
		if(alineacionTexto.equals("TopRight")){
			
			horizontalTexto = SwingConstants.RIGHT;
			verticalTexto = SwingConstants.TOP;
			
		}
		
		if(alineacionTexto.equals("MiddleLeft")){
			
			horizontalTexto = SwingConstants.LEFT;
			verticalTexto = SwingConstants.CENTER;
			
		}
		
		if(alineacionTexto.equals("MiddleCenter")){
			
			horizontalTexto = SwingConstants.CENTER;
			verticalTexto = SwingConstants.CENTER;
			
		}
		
		if(alineacionTexto.equals("MiddleRight")){
			
			horizontalTexto = SwingConstants.RIGHT;
			verticalTexto = SwingConstants.CENTER;
			
		}
		
		if(alineacionTexto.equals("BottomLeft")){
			
			horizontalTexto = SwingConstants.LEFT;
			verticalTexto = SwingConstants.BOTTOM;
			
		}
		
		if(alineacionTexto.equals("BottomCenter")){
			
			horizontalTexto = SwingConstants.CENTER;
			verticalTexto = SwingConstants.BOTTOM;
			
		}
		
		if(alineacionTexto.equals("BottomRight")){
			
			horizontalTexto = SwingConstants.RIGHT;
			verticalTexto = SwingConstants.BOTTOM;
			
		}
		
		
		if(alineacionImagen.equals("TopLeft")){
			
			horizontalImagen = SwingConstants.LEFT;
			verticalImagen = SwingConstants.TOP;
			
		}
		
		if(alineacionImagen.equals("TopCenter")){
			
			horizontalImagen = SwingConstants.CENTER;
			verticalImagen = SwingConstants.TOP;
			
		}
		
		if(alineacionImagen.equals("TopRight")){
			
			horizontalImagen = SwingConstants.RIGHT;
			verticalImagen = SwingConstants.TOP;
			
		}
		
		if(alineacionImagen.equals("MiddleLeft")){
			
			horizontalImagen = SwingConstants.LEFT;
			verticalImagen = SwingConstants.CENTER;
			
		}
		
		if(alineacionImagen.equals("MiddleCenter")){
			
			horizontalImagen = SwingConstants.CENTER;
			verticalImagen = SwingConstants.CENTER;
			
		}
		
		if(alineacionImagen.equals("MiddleRight")){
			
			horizontalImagen = SwingConstants.RIGHT;
			verticalImagen = SwingConstants.CENTER;
			
		}
		
		if(alineacionImagen.equals("BottomLeft")){
			
			horizontalImagen = SwingConstants.LEFT;
			verticalImagen = SwingConstants.BOTTOM;
			
		}
		
		if(alineacionImagen.equals("BottomCenter")){
			
			horizontalImagen = SwingConstants.CENTER;
			verticalImagen = SwingConstants.BOTTOM;
			
		}
		
		if(alineacionImagen.equals("BottomRight")){
			
			horizontalImagen = SwingConstants.RIGHT;
			verticalImagen = SwingConstants.BOTTOM;
			
		}
		
		
		
	}

	public Color getColorPresionado() {
		return colorPresionado;
	}

	public void setColorPresionado(Color colorPresionado) {
		this.colorPresionado = colorPresionado;
	}
	
	
	
}
